package com.algo.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.sql.DataSource;

/**
 * @Profile 可根据当前的环境激活指定的bean
 * 如果不配置当前处于什么环境，默认激活 "default" 指定的bean
 *
 * profile设置的两种方式：
 *      第一种：通过启动参数：-Dspring.profiles.active="XXX"
 *      第二种，构建ApplicationContext时，通过environment设置activeProfiles
 *
 */
@Configuration
public class MainConfigOfProfile {

    @Value("${datasouce.username}")
    private String userName;

    @Value("${datasource.password}")
    private String password;

    @Value("${datasource.url}")
    private String url;

    @Bean
    @Profile("default")
    public DataSource defaultDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setUrl(url);

        return dataSource;
    }

    @Bean
    @Profile("test")
    public DataSource testDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setUrl(url);

        return dataSource;
    }

    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setUrl(url);

        return dataSource;
    }

    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setUrl(url);

        return dataSource;
    }


}
